package com.library.vo;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class P<T> implements Serializable {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1L;


    /**
     * 当前页码
     */
    private int pageIndex;


    /**
     * 单页面记录总数
     */
    private int pageSize;

    /**
     * 总页数
     */
    private int pageTotal;

    /**
     * 总记录数
     */
    private long recordTotal;


    private T data;


    private P() {

    }

    public P(Page<T> page) {
        if (null != page) {
            data = (T) page.getResult();
            pageIndex = page.getPageNum();
            pageSize = page.getPageSize();
            pageTotal = page.getPages();
            recordTotal = page.getTotal();
        }
    }

    /**
     * 记录数数据
     *
     * @param pageIndex
     * @param pageSize
     * @param pageTotal
     * @param recordTotal
     * @param t
     */
    public P(int pageIndex, int pageSize, int pageTotal, long recordTotal, T t) {
        this.data = t;
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.pageTotal = pageTotal;
        this.recordTotal = recordTotal;
    }

    /**
     * 只返回记录数和数据
     *
     * @param recordTotal
     * @param t
     */
    public P(long recordTotal, T t) {
        this.data = t;
        this.recordTotal = recordTotal;
    }

    /**
     * 包裹分页总数和分页数据xQ
     *
     * @param recordTotal
     * @param t
     * @param <T>
     * @return
     */
    public static <T> P<T> valueOf(int pageIndex, int pageSize, int pageTotal, long recordTotal, T t) {
        P<T> p = new P<T>();
        if (null != t) {
            p.setData(t);
            p.setPageIndex(pageIndex);
            p.setPageSize(pageSize);
            p.setPageTotal(pageTotal);
            p.setRecordTotal(recordTotal);
        }
        return p;
    }

    /**
     * 包裹分页总数和分页数据
     *
     * @param recordTotal
     * @param t
     * @param <T>
     * @return
     */
    public static <T> P<T> valueOf(long recordTotal, T t) {
        P<T> p = new P<T>();
        if (null != t) {
            p.setData(t);
            p.setRecordTotal(recordTotal);
        }
        return p;
    }

    /**
     * 包裹分页数据
     *
     * @param page
     * @param <T>
     * @return
     */
    public static <T> P<T> valueOf(Page<T> page) {
        P<T> p = new P<T>();
        if (null != page) {
            p.setData((T) page.getResult());
            p.setPageIndex(page.getPageNum());
            p.setPageSize(page.getPageSize());
            p.setPageTotal(page.getPages());
            p.setRecordTotal(page.getTotal());
        }
        return p;
    }

    /**
     * 包裹分页数据
     *
     * @param pageInfo
     * @param <T>
     * @param <F>
     * @return
     */
    public static <T, F> P<T> valueOf(PageInfo<F> pageInfo) {
        P<T> p = new P<T>();
        if (null != pageInfo) {
            p.setData((T) pageInfo.getList());
            p.setPageIndex(pageInfo.getPageNum());
            p.setPageSize(pageInfo.getPageSize());
            p.setPageTotal(pageInfo.getPages());
            p.setRecordTotal(pageInfo.getTotal());
        }
        return p;
    }

    /**
     * 或许map形式的分页
     *
     * @return
     */
    public Map<String, Object> page() {
        Map<String, Object> page = new HashMap<>();
        page.put("pageIndex", this.getPageIndex());
        page.put("pageTotal", this.getPageTotal());
        page.put("pageSize", this.getPageSize());
        page.put("recordTotal", this.getRecordTotal());
        return page;
    }

    /**
     * 获取分页包裹的数据
     *
     * @return
     */
    public T data() {
        return data;
    }


    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageTotal() {
        return pageTotal;
    }

    public void setPageTotal(int pageTotal) {
        this.pageTotal = pageTotal;
    }

    public long getRecordTotal() {
        return recordTotal;
    }

    public void setRecordTotal(long recordTotal) {
        this.recordTotal = recordTotal;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


}
